<template>
  <el-popover
      ref="popoverRef" :show-arrow="false" :offset="6"
      placement="bottom-start" trigger="click" :hide-after="0" :teleported="false"
  >
    <template #reference>
      <slot />
    </template>
    <template #default class="search-engines-list">
      <div class="more-menu">
        <div class="more-menu-box">
          <div
              class="menu-item"
              v-for="(item, index) in useStore.searchEngines"
              :key="item.id"
              @click="changeSearchEngine(index)"
          >
            <div class="icon">
              <i class="iconfont" :class="item.icon"></i>
            </div>
            <span>{{item.engineName}}</span>
          </div>
        </div>
      </div>
    </template>
  </el-popover>
</template>

<script setup>
import {ref} from "vue";
import {useMenuStore} from "@/stores/menu";

const popoverRef = ref()
const useStore = useMenuStore()

const changeSearchEngine = (index) => {
  popoverRef.value.hide();
  useStore.updateSearchIndex(index);
  useStore.searchPlaceholder = useStore.searchEngines[useStore.searchIndex].engineName;
};
</script>

<style lang="less" scoped>
.menu-item {
  border-radius: 4px;
  padding: 0 8px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  cursor: pointer;

  &:hover {
    background-color: rgba(0, 0, 0, .05);
  }

  .icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 25px;
    height: 25px;
    margin-right: 8px;
  }
}
</style>

<style>
#search-bar .el-popover.el-popper {
  min-width: 0;
  width: auto!important;
}
</style>